package com.farm.kaiping.controller;


import com.farm.kaiping.entity.Pig;
import com.farm.kaiping.po.PigPO;
import com.farm.kaiping.service.PigLogicService;
import com.farm.kaiping.service.PigService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 猪只信息表(Pig)表控制层
 *
 * @author makejava
 * @since 2025-01-22 13:56:28
 */
@RestController
@RequestMapping("/pig")
@AllArgsConstructor
@Api(tags = "猪只信息表控制器")
public class PigController {

	private final PigService pigService;
	private final PigLogicService pigLogicService;

	@ApiOperation(value = "分页查询猪只信息表", notes = "权限：view_pig_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_page')")
	@PostMapping("/page")
	public R<Page<Pig>> getPigPage(@RequestBody PigPO pigPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(pigPO);
		return R.ok(pigService.getPigPage(page, pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询猪只信息表列表", notes = "权限：view_pig_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_list')")
	@PostMapping("/list")
	public R<List<PigPO>> getPigList(@RequestBody PigPO pigPO) {
		try{
			return R.ok(pigLogicService.getPigList(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询猪只信息表", notes = "权限 view_pig_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_view')" )
	public R<Pig> getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(pigService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条猪只信息表", notes = "权限：view_pig_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_one')")
	@PostMapping("/one")
	public R<Pig> getPig(@RequestBody PigPO pigPO) {
		try{
			return R.ok(pigService.getPig(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增猪只信息表", notes = "权限：view_pig_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_add')")
	@PostMapping("/add")
	public R<Integer> savePig(@RequestBody PigPO pigPO) {
		try{
		    return R.ok(pigService.savePig(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改猪只信息表", notes = "权限：view_pig_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_edit')")
	@PostMapping("/edit")
	public R<String> updatePig(@RequestBody PigPO pigPO) {
		try{
		    return R.ok(pigService.updatePig(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除猪只信息表", notes = "权限：view_pig_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_del')")
	@PostMapping("/del")
	public R<String> removePig(@RequestBody PigPO pigPO) {
		try{
		    return R.ok(pigService.removePig(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	@ApiOperation(value = "生猪批次入圈", notes = "权限：view_pig_batchInPen")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_batchInPen')")
	@PostMapping("/batch-in-pen")
	public R batchInPen(@RequestBody PigPO pigPO) {
		try{
			return R.ok(pigLogicService.batchInPen(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "按批次修改", notes = "权限：view_pig_editByBatch")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_pig_editByBatch')")
	@PostMapping("/edit-by-batch")
	public R<String> batchUpdate(@RequestBody PigPO pigPO) {
		try{
			return R.ok(pigService.batchUpdate(pigPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
}

